package com.gfxy.modules.items.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gfxy.modules.items.entity.Jxnlds;
import com.gfxy.modules.items.entity.teachingresult.TeachingResultContent;
import com.gfxy.modules.items.mapper.TJxnldsMapper;
import com.gfxy.modules.items.service.ITJxnldsService;
import com.gfxy.modules.items.vo.*;
import com.gfxy.modules.excel.ExcelUtil;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 教学能力大赛
 * @Author: jeecg-boot
 * @Date:   2021-08-28
 * @Version: V1.0
 */
@Service
public class JxnldsServiceImpl extends AbstractProjectServiceImpl<TJxnldsMapper, Jxnlds> implements ITJxnldsService {

	protected int memberCellNum = 4;

	@Override
	protected void pass(String id, String isPass) {

	}

	@Override
	protected String[] getMemberArray(Row row) {
		String nameStrs = ExcelUtil.formatCell(row.getCell(memberCellNum));
		return nameStrs.split(memberNameSeparator);
	}

	@Override
	protected Jxnlds getOneProject(Row row) {

		String date = ExcelUtil.formatCell(row.getCell(0));

		String classess  = ExcelUtil.formatCell(row.getCell(1));
		String award= ExcelUtil.formatCell(row.getCell(2));
		String projectName = ExcelUtil.formatCell(row.getCell(3));

		Jxnlds jxnlds = new Jxnlds();

		jxnlds.setDate(date);
		jxnlds.setAward(translateaward(award));
		jxnlds.setClassess(translateClassess(classess));
		jxnlds.setProjectName(projectName);
		jxnlds.setIsPass(0);
		//这个地方不要忘了-算总分
		jxnlds.setTotalScore(this.getTotalScore(jxnlds));
		return jxnlds;
	}


	@Override
	public Double getTotalScore(Jxnlds jxnlds) {

		double result = 0.0;
		int award = jxnlds.getAward();
		int classess = jxnlds.getClassess();
		if(classess == 1){
			if(award == 1){
				result = 200;
			} else if(award == 2 ) {
				result = 150;
			} else if(award == 3 ) {
				result = 120;
			}
		} else if(classess == 2){
			if(award == 1){
				result = 100;
			} else if(award == 2 ) {
				result = 70;
			} else if(award == 3 ) {
				result = 40;
			}
		} else if(classess == 3){
			if(award == 1){
				result = 30;
			} else if(award == 2 ) {
				result = 20;
			} else if(award == 3 ) {
				result = 10;
			}
		}

		return result;
	}

	private int translateClassess(String name){
		if(name == null || name.equals("")){
			return 0;
		} else if(name.equals("国家级")){
			return 1;
		} else if(name.equals("省级")){
			return 2;
		} else if(name.equals("校级")){
			return 3;
		} else {
			return 0;
		}

	}
	private int translateaward(String award) {
		if (award == null || award.equals("") ) {
			return 0;
		} else if (award.equals("一等奖")) {
			return 1;
		} else if (award.equals("二等奖")) {
			return 2;
		}else if (award.equals("三等奖")){
			return 3;
		}else {
			return 0;
		}
	}


	@Override
	public double computeProportion(int ranked, int totalNum) {
		if(totalNum==1){
			return 1.0;
		} else if(totalNum == 2){
			if(ranked ==1){
				return 0.6;
			} else if(ranked == 2){
				return 0.4;
			} else {
				return 0.0;
			}
		} else if(totalNum == 3){
			if(ranked ==1){
				return 0.5;
			} else if(ranked == 2){
				return 0.25;
			} else if(ranked == 3){
				return 0.25;
			} else {
				return 0.0;
			}
		} else if(totalNum == 4){
			if(ranked ==1){
				return 0.4;
			} else if(ranked == 2){
				return 0.2;
			} else if(ranked == 3){
				return 0.2;
			} else if(ranked == 4){
				return 0.2;
			} else {
				return 0.0;
			}
		} else if(totalNum >= 5){
			if(ranked == 1){
				return 0.5;
			} else if(ranked > 1){
				return Math.floor(0.5*10000/(totalNum-1))/10000.0 ;
			} else {
				return 0.0;
			}
		} else {
			return 0.0;
		}
	}

	@Override
	public IPage<TJxnldsMenberPage> getJxnldsMemberPageListByYear(String date, Integer pageNo, Integer pageSize) {
		Page<TJxnldsMenberPage> page = new Page<>(pageNo,pageSize);
		return page.setRecords(this.baseMapper.selectJxnldsMenberPageListByYear(date));
	}

	@Override
	public List<TJxnldsMenberPage> getJxnldsMemberPageListByYear(String date) {
		return this.baseMapper.selectJxnldsMenberPageListByYear(date);
	}

	@Override
	protected List<TeachingResultContent> createTeachingResultContent(Jxnlds jxcg)  {

		List<TeachingResultContent> result = new ArrayList<>();

		// 类型
		TeachingResultContent classTeachingResultContent = new TeachingResultContent();
		classTeachingResultContent.setType(1);
		classTeachingResultContent.setLevel(jxcg.getClassess());

		try {
			classTeachingResultContent.setValue(dictTranslater.getDictValue(TJxnldsMenberPage.class, "classess", String.valueOf(jxcg.getClassess())));
			result.add(classTeachingResultContent);
		} catch (NoSuchFieldException e) {
			log.error(e.getMessage(),e);
		}

		//奖项
		TeachingResultContent awardTeachingResultContent = new TeachingResultContent();
		awardTeachingResultContent.setType(1);
		awardTeachingResultContent.setLevel(jxcg.getAward());
		try {
			awardTeachingResultContent.setValue(dictTranslater.getDictValue(TJxnldsMenberPage.class, "award", String.valueOf(jxcg.getAward())));
			result.add(awardTeachingResultContent);
		} catch (NoSuchFieldException e) {
			log.error(e.getMessage(),e);
		}

		return result;
	}
}
